package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Author:L枣
 * Date:2024/9/19
 */
@Component

public class OrderStatisticsTask {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    //每天凌晨两点，查询前一天统计数据，把统计之后的数据添加到统计结果表里面

    @Scheduled(cron = "0 0 2 * * ?")
    //@Scheduled(cron = "0/10 * * * * ?")
    public void orderTotalAmountStatistics(){
        System.out.println("开始统计前一天的数据...");
        //1、获取前一天日期
        String createDate = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");

        //2、根据前一天的日期进行统计功能（分组操作）
            //统计前一天交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectStatisticsByDate(createDate);

        //3、把统计结果添加到统计结果表里面
        if (orderStatistics != null){
            orderStatisticsMapper.insert(orderStatistics);
        }
    }

}
